package com.example.service.impl.pc;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.entity.hz.MOCTA;
import com.example.entity.vo.pc.AutoBTdan;
import com.example.entity.vo.pc.AutoBTdanDto;
import com.example.entity.vo.pc.AutoGenLL;
import com.example.entity.vo.pc.AutoGenLLDto;
import com.example.entity.yifei.*;
import com.example.entity.ymlConfig.YmlConfig;
import com.example.exception.CustomException;
import com.example.mapper.DbtempMapper;
import com.example.mapper.MoctbMapper;
import com.example.mapper.MoctcMapper;
import com.example.mapper.pc.LingLiaoMapper;
import com.example.service.*;
import com.example.service.pc.LingLiaoService;
import com.example.untils.SecurityUtils;
import com.example.untils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

@Service
public class LingLiaoServiceImpl implements LingLiaoService {

    @Autowired
    private LingLiaoMapper lingLiaoMapper;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private DbtempService dbtempService;

    @Autowired
    private DbtempMapper dbtempMapper;

    @Autowired
    private MoctcMapper moctcMapper;

    @Autowired
    private MoctbMapper moctbMapper;

    @Autowired
    private CmsmqService cmsmqService;

    @Autowired
    private CmsmaService cmsmaService;

    @Autowired
    private MoctaService moctaService;

    @Autowired
    private MocteService mocteService;

    @Autowired
    private InvmbService invmbService;


    @Override
    public void gongdanLL(AutoGenLLDto dto) {

    }

    @Override
    public void paichanLL(AutoGenLLDto dto) {

    }

    /**
     *
     * @param dto
     * @param isZZ  是否为自制领料
     * @return
     */
    @Override
    public String autoBTdan(AutoBTdanDto dto, Boolean isZZ) {

        List<Cmsmq> cmsmqList = cmsmqService.list();
        Map<String, String> cmsmqMap = new HashMap<>();
        for(Cmsmq cmsmq : cmsmqList) {
            cmsmqMap.put(cmsmq.getMq001().trim(), cmsmq.getMq002().trim());
        }
        String zzLL = cmsmqMap.get("材料领料单");   String wwLL = cmsmqMap.get("委外领料单");
//        String zzBL = cmsmqMap.get("材料补料单");   String wwBL = cmsmqMap.get("委外补料单");
//        String zzTL = cmsmqMap.get("材料退料单");   String wwTL = cmsmqMap.get("委外退料单");
        String hlTK = cmsmqMap.get("工单回料退库单");

        dto.setCurUser(SecurityUtils.getLoginUser().getUsername());
        StringBuffer res = new StringBuffer();
        //(自制，委外)的补料单别
        Cmsmq bldb = cmsmqService.getOne(new LambdaQueryWrapper<Cmsmq>().eq(Cmsmq::getMq001, dto.getLlType()));
        // 保存几位小数用
        Cmsma cmsma = cmsmaService.getOne(new LambdaQueryWrapper<Cmsma>().eq(Cmsma::getCompany, YmlConfig.getYFCOMPANY()));
        List<AutoBTdan> yyyy = dto.getCheckedList();
        List<AutoBTdan> bldList = new ArrayList<>();
        List<AutoBTdan> tldList = new ArrayList<>();
        List<AutoBTdan> hltkdList = new ArrayList<>();
        // MOCTB.UDF05 标记一下是否是后来补充的
        if(dto.getIsLL()) {     // 生成领料单的时候
            for(AutoBTdan autoBTdan : yyyy) {
                if(autoBTdan.getUnReceivedNums() < 0) {   // 退料单
                    throw new CustomException("500", "生成领料单时, 数量不能为负数。");
                }else if(autoBTdan.getUnReceivedNums() > 0) {  // 补料单
                    bldList.add(autoBTdan);
                }
            }
        } else {   // 生成补/退料单的时候
            for(AutoBTdan autoBTdan : yyyy) {
                int count = mocteService.count(new LambdaQueryWrapper<Mocte>().eq(Mocte::getTe011, autoBTdan.getType()).eq(Mocte::getTe012, autoBTdan.getId()).and(wrapper -> wrapper.eq(Mocte::getTe001, zzLL).or().eq(Mocte::getTe001, wwLL)));
                if(count == 0) {
                    throw new CustomException("500", autoBTdan.getType() + "-" + autoBTdan.getId() + "没有领料单");
                }
                if(autoBTdan.getUnReceivedNums() < 0) {   // 退料单
                    Integer count1 = moctbMapper.selectCount(new LambdaQueryWrapper<Moctb>().eq(Moctb::getTb001, autoBTdan.getType()).eq(Moctb::getTb002, autoBTdan.getId()).eq(Moctb::getTb003, autoBTdan.getProductId())
                            .and(wrapper -> wrapper.eq(Moctb::getUdf05, "").or().isNull(Moctb::getUdf05)));
                    autoBTdan.setUnReceivedNums(Math.abs(autoBTdan.getUnReceivedNums()));
                    if(count1 != null && count1 > 0) {
                        tldList.add(autoBTdan);
                    }else{
                        hltkdList.add(autoBTdan);
                    }
                }else if(autoBTdan.getUnReceivedNums() > 0) {  // 补料单
                    bldList.add(autoBTdan);
                }
            }
        }

        // 补料单
        dbtempService.remove(new LambdaQueryWrapper<Dbtemp>().eq(Dbtemp::getLrz, dto.getCurUser()));
        for(AutoBTdan t : bldList) {
            if(dbtempService.count(new LambdaQueryWrapper<Dbtemp>().eq(Dbtemp::getLrz, dto.getCurUser()).eq(Dbtemp::getCkbh, t.getStockId())) == 0) {
                dbtempService.save(new Dbtemp(dto.getCurUser(), t.getStockId(), dto.getLlType(), ""));
            }
        }
        List<Dbtemp> list = dbtempService.list(new LambdaQueryWrapper<Dbtemp>().eq(Dbtemp::getLrz, dto.getCurUser()));
        Set<String> ckbhSet = new HashSet<>();
        for(Dbtemp t : list) {
            ckbhSet.add(t.getCkbh());
        }
        // 插入单头
        for (String ckbh : ckbhSet) {
            String moctcId = cmsmqService.getIdByType(dto.getLlType(), "MOCTC");    // 单号
//            if(isZZ)
            moctcMapper.insertMoctc(dto.getCurUser(), dto.getLlType(), moctcId, bldList.get(0).getWorkCenterId(), bldList.get(0).getSupplierId(), isZZ ? "54" : "55", dto.getLlDate());
//            else moctcMapper.insertMoctc(dto.getCurUser(), dto.getLlType(), moctcId, bldList.get(0).getWorkCenterId(), bldList.get(0).getSupplierId(), "55", dto.getLlDate());

            if(dto.getIsLL()) res.append("领料单: ").append(dto.getLlType()).append("-").append(moctcId).append("\n");
            else res.append("补料单: ").append(dto.getLlType()).append("-").append(moctcId).append("\n");
            dbtempService.update(new Dbtemp(null, null, null, moctcId), new LambdaQueryWrapper<Dbtemp>().eq(Dbtemp::getLrz, dto.getCurUser()).eq(Dbtemp::getCkbh, ckbh));
        }
        // 插入单身
        for(AutoBTdan t : bldList) {
            Moctb moctb = moctbMapper.selectOne(new LambdaQueryWrapper<Moctb>().eq(Moctb::getTb001, t.getType()).eq(Moctb::getTb002, t.getId()).eq(Moctb::getTb003, t.getProductId())
                    .and(wrapper -> wrapper.eq(Moctb::getUdf05, "").or().isNull(Moctb::getUdf05)));
            if(dto.getIsLL()) {    // 领料单
                // 看一下领的料在不在单身中 不在的料 需要存回MOCTB
                if(moctb == null) {
                    throw new CustomException("500", "工单: " + t.getType() + "-" + t.getId() + " 中不存在材料:  "+ t.getProductId() + "。需生成补料单！");
                }
                BigDecimal ylsl = lingLiaoMapper.ylsl(t.getType(), t.getId(), t.getProductId());
                BigDecimal allNums = new BigDecimal(moctb.getTb004());
                BigDecimal can = allNums.subtract(ylsl).setScale(Integer.parseInt(cmsma.getMa068()), RoundingMode.HALF_UP);  // 最多能领的数量
                if(can.compareTo(new BigDecimal(t.getUnReceivedNums())) < 0) {
                    throw new CustomException("500", "工单" + t.getType() + "-" + t.getId() + "的材料 "+ t.getProductId() + "最多只能领：" + can + ", 需要领料生成补料单。");
                }
            } else {   // 补料单
                if(!bldb.getMq041().trim().equals("N") && moctb != null) {
                    Invmb invmb = invmbService.getOne(new LambdaQueryWrapper<Invmb>().eq(Invmb::getMb001, t.getProductId()));
                    BigDecimal mb401 = invmb.getMb401();    // 超领率
                    BigDecimal bl = new BigDecimal( 1).add(mb401);  // 比例
                    // 查出现在已经领了多少料了(已领数量)
                    BigDecimal ylsl = lingLiaoMapper.ylsl(t.getType(), t.getId(), t.getProductId());
                    BigDecimal allNums = new BigDecimal(moctb.getTb004());
                    BigDecimal almost = allNums.multiply(bl).setScale(Integer.parseInt(cmsma.getMa068()), RoundingMode.HALF_UP);   // 乘上比例后的最大数量值
                    BigDecimal can = almost.subtract(ylsl).setScale(Integer.parseInt(cmsma.getMa068()), RoundingMode.HALF_UP);  // 最多能领的数量
                    if(can.compareTo(new BigDecimal(t.getUnReceivedNums())) < 0) {
                        throw new CustomException("500", "工单" + t.getType() + "-" + t.getId() + "的材料 "+ t.getProductId() + "最多只能领：" + can);
                    }
                }
                if(moctb == null) {    // 需要回写MOCTB
                    Moctb y = moctbMapper.selectOne(new LambdaQueryWrapper<Moctb>().eq(Moctb::getTb001, t.getType()).eq(Moctb::getTb002, t.getId()).eq(Moctb::getTb003, t.getProductId())
                            .eq(Moctb::getUdf05, "Y"));
                    if(y == null) {  // 如果是第一次则是新增
                        Invmb invmb = invmbService.getOne(new LambdaQueryWrapper<Invmb>().eq(Invmb::getMb001, t.getProductId().trim()));
                        y = new Moctb();
                        y.setTb001(t.getType());  y.setTb002(t.getId());  y.setTb003(t.getProductId());
                        y.setTb004(0D);  y.setTb005(t.getUnReceivedNums());
                        y.setTb009(invmb.getMb017());  // 仓库
                        y.setTb011("1");  y.setTb012(invmb.getMb002());  y.setTb013(invmb.getMb003());
                        y.setTb017("由补料时产生");  y.setTb018("N");  // 审核码
                        y.setTb038("********************");  y.setUdf05("Y");  // 标记为是后续生成的
                        moctbMapper.insert(y);
                    }else{  // 如果不是第一次，则是修改
                        y.setTb005(y.getTb005() + t.getUnReceivedNums());
                        moctbMapper.update(y, new LambdaQueryWrapper<Moctb>().eq(Moctb::getTb001, t.getType()).eq(Moctb::getTb002, t.getId()).eq(Moctb::getTb003, t.getProductId())
                                .eq(Moctb::getUdf05, "Y"));
                    }

                }
            }
            moctcMapper.insertMocte(t, dto.getCurUser());
            Dbtemp one = dbtempService.getOne(new LambdaQueryWrapper<Dbtemp>().eq(Dbtemp::getLrz, dto.getCurUser()).eq(Dbtemp::getCkbh, t.getStockId()));
            Moctc moctc = new Moctc();  moctc.setTc019(t.getType());  moctc.setTc020(t.getId());
            moctcMapper.update(moctc, new LambdaQueryWrapper<Moctc>().eq(Moctc::getTc001, one.getTa001()).eq(Moctc::getTc002, one.getTa002()));
        }
        dbtempService.remove(new LambdaQueryWrapper<Dbtemp>().eq(Dbtemp::getLrz, dto.getCurUser()));

        // 生成退料单单据
        for(AutoBTdan t : tldList) {
            if(dbtempService.count(new LambdaQueryWrapper<Dbtemp>().eq(Dbtemp::getLrz, dto.getCurUser()).eq(Dbtemp::getCkbh, t.getStockId())) == 0) {
                dbtempService.save(new Dbtemp(dto.getCurUser(), t.getStockId(), dto.getTlType(), ""));
            }
        }
        list = dbtempService.list(new LambdaQueryWrapper<Dbtemp>().eq(Dbtemp::getLrz, dto.getCurUser()));
        ckbhSet = new HashSet<>();
        for(Dbtemp t : list) {
            ckbhSet.add(t.getCkbh());
        }
        // 插入单头
        for (String ckbh : ckbhSet) {
            String moctcId = cmsmqService.getIdByType(dto.getTlType(), "MOCTC");
            if(isZZ) moctcMapper.insertMoctcTLD(dto.getCurUser(), dto.getTlType(), moctcId, tldList.get(0).getWorkCenterId(), tldList.get(0).getSupplierId(), "56", dto.getLlDate());
            else moctcMapper.insertMoctcTLD(dto.getCurUser(), dto.getTlType(), moctcId, tldList.get(0).getWorkCenterId(), tldList.get(0).getSupplierId(), "57", dto.getLlDate());
            res.append("退料单: ").append(dto.getTlType()).append("-").append(moctcId).append("\n");
            dbtempService.update(new Dbtemp(null, null, null, moctcId),
                    new LambdaQueryWrapper<Dbtemp>().eq(Dbtemp::getLrz, dto.getCurUser()).eq(Dbtemp::getCkbh, ckbh));
        }
        // 插入单身
        for(AutoBTdan t : tldList) {
            // 看一下退的料在不在单身中 不在的料  需要生成（工单回料退库单），否则生成普通的退料单
            moctcMapper.insertMocteTLD(t, dto.getCurUser());
            Dbtemp one = dbtempService.getOne(new LambdaQueryWrapper<Dbtemp>().eq(Dbtemp::getLrz, dto.getCurUser()).eq(Dbtemp::getCkbh, t.getStockId()));
            Moctc moctc = new Moctc();  moctc.setTc019(t.getType());  moctc.setTc020(t.getId());
            moctcMapper.update(moctc, new LambdaQueryWrapper<Moctc>().eq(Moctc::getTc001, one.getTa001()).eq(Moctc::getTc002, one.getTa002()));
        }
        dbtempService.remove(new LambdaQueryWrapper<Dbtemp>().eq(Dbtemp::getLrz, dto.getCurUser()));

        // 生成回料退库单
        for(AutoBTdan t : hltkdList) {
            if(dbtempService.count(new LambdaQueryWrapper<Dbtemp>().eq(Dbtemp::getLrz, dto.getCurUser()).eq(Dbtemp::getCkbh, t.getStockId())) == 0) {
                dbtempService.save(new Dbtemp(dto.getCurUser(), t.getStockId(), hlTK, ""));
            }
        }
        list = dbtempService.list(new LambdaQueryWrapper<Dbtemp>().eq(Dbtemp::getLrz, dto.getCurUser()));
        ckbhSet = new HashSet<>();
        for(Dbtemp t : list) {
            ckbhSet.add(t.getCkbh());
        }
        // 插入单头
        for (String ckbh : ckbhSet) {
            String moctcId = cmsmqService.getIdByType(hlTK, "MOCTC");
            if(isZZ) moctcMapper.insertMoctcTLD(dto.getCurUser(), hlTK, moctcId, tldList.get(0).getWorkCenterId(), tldList.get(0).getSupplierId(), "56", dto.getLlDate());
            else moctcMapper.insertMoctcTLD(dto.getCurUser(), hlTK, moctcId, tldList.get(0).getWorkCenterId(), tldList.get(0).getSupplierId(), "57", dto.getLlDate());
            res.append("回料退库单: ").append(hlTK).append("-").append(moctcId).append("\n");
            dbtempService.update(new Dbtemp(null, null, null, moctcId),
                    new LambdaQueryWrapper<Dbtemp>().eq(Dbtemp::getLrz, dto.getCurUser()).eq(Dbtemp::getCkbh, ckbh));
        }
        // 插入单身
        for(AutoBTdan t : hltkdList) {
            // 看一下退的料在不在单身中 不在的料  需要生成（工单回料退库单），否则生成普通的退料单

//            Moctb y = moctbMapper.selectOne(new LambdaQueryWrapper<Moctb>().eq(Moctb::getTb001, t.getType()).eq(Moctb::getTb002, t.getId()).eq(Moctb::getTb003, t.getProductId())
//                    .eq(Moctb::getUdf05, "Y"));

            moctcMapper.insertMocteTLD(t, dto.getCurUser());
            Dbtemp one = dbtempService.getOne(new LambdaQueryWrapper<Dbtemp>().eq(Dbtemp::getLrz, dto.getCurUser()).eq(Dbtemp::getCkbh, t.getStockId()));
            Moctc moctc = new Moctc();  moctc.setTc019(t.getType());  moctc.setTc020(t.getId());
            moctcMapper.update(moctc, new LambdaQueryWrapper<Moctc>().eq(Moctc::getTc001, one.getTa001()).eq(Moctc::getTc002, one.getTa002()));
        }
        dbtempService.remove(new LambdaQueryWrapper<Dbtemp>().eq(Dbtemp::getLrz, dto.getCurUser()));
        return res.toString();
    }

    /**
     * 自动领料
     * @param list   要领料的数组
     * @param llType   领料单单别
     * @param llTypeNo  领料类型  54 55
     * @param isMocta   是否走的按工单领料
     */
    @Override
    public String gongdanAutoLL(List<AutoGenLL> list, String llType, String llTypeNo, Boolean isMocta, String llDate) {
        StringBuffer res = new StringBuffer();
        Map<String, BigDecimal> map = new HashMap<>();  // 做工单工序和数量的映射关系
        if(isMocta == false) {  // 走排产领料
            for(AutoGenLL t : list) {
                map.put(t.getUuid().trim(), new BigDecimal(t.getNums()));
//                map.put(t.getType()+t.getId()+"****", new BigDecimal(t.getNums()));
            }
        }
        Cmsma cmsma = cmsmaService.getOne(new LambdaQueryWrapper<Cmsma>().eq(Cmsma::getCompany, YmlConfig.getYFCOMPANY()));
        List<String> ckList = null;
        if(isMocta) ckList = lingLiaoMapper.getCkListByTypeIdList(list);
        else ckList = lingLiaoMapper.ckListMoctyList(list);
        MOCTA mocta = moctaService.getOne(new LambdaQueryWrapper<MOCTA>().eq(MOCTA::getTA001, list.get(0).getType()).eq(MOCTA::getTA002, list.get(0).getId()));
        for(String ckId : ckList) {
            Moctc moctc = new Moctc();
            moctc.setCompany(mocta.getCOMPANY());
            moctc.setCreator(SecurityUtils.getLoginUser().getUsername());
            moctc.setCreateDate(TimeUtils.getNowTime());
            moctc.setUsrGroup(mocta.getUSR_GROUP());
            moctc.setFlag(1D);
            moctc.setTc001(llType);
            moctc.setTc002(cmsmqService.getIdByType(moctc.getTc001(), "MOCTC"));
            res.append("领料单: ").append(moctc.getTc001()).append("-").append(moctc.getTc002()).append("\n");
//            moctc.setTc003(TimeUtils.HZStdTime());
            moctc.setTc003(TimeUtils.HZStdTime());
            moctc.setTc004(mocta.getTA019());
            moctc.setTc005(mocta.getTA021());
            moctc.setTc006(mocta.getTA032());
            moctc.setTc007("");
            moctc.setTc008(llTypeNo);
            moctc.setTc009("N");
            moctc.setTc010(0D);
            moctc.setTc011("N");
            moctc.setTc012("2");
            moctc.setTc013("Y");
            moctc.setTc014(llDate);
            moctc.setTc015("");
            moctc.setTc016("N");
            moctc.setTc017(0D);
            moctc.setTc018("1");
            moctc.setTc019("");
            moctc.setTc020("");
            moctc.setTc031("********************");
            moctc.setUdf52(mocta.getTA015().doubleValue());
//                moctc.setUdf01();
            moctc.setUdf02(mocta.getTA034());
            moctc.setUdf03(mocta.getTA035());
            moctc.setUdf04(lingLiaoMapper.moctcUdf04(mocta.getTA001(), mocta.getTA002()));
            moctc.setUdf05(mocta.getTA027());
            moctcMapper.insert(moctc);

            List<Moctb> moctbList = null;
            if(isMocta) moctbList = lingLiaoMapper.llMoctbListByTb009List(list, ckId);
            else moctbList = lingLiaoMapper.llMoctbListByMoctyList(list, ckId);
            int xh = 1;
            for(Moctb moctb : moctbList) {
                // 查看品号在一个仓库中，已领料但是未审核的数量
                Double ylUncheckNums = lingLiaoMapper.uncheckNumsByMocte(moctb.getTb003(), moctb.getTb009());
                if(ylUncheckNums == null) ylUncheckNums = new Double(0);
                // 查看库存数量
                Double yksl = lingLiaoMapper.stockNum(moctb.getTb003(), moctb.getTb009());
                if(yksl == null) yksl = new Double(0);
                Mocte mocte = new Mocte();
                mocte.setCompany(moctb.getCompany());
                mocte.setCreator(SecurityUtils.getLoginUser().getUsername());
                mocte.setCreateDate(TimeUtils.getNowTime());
                mocte.setUsrGroup(moctb.getUsrGroup());
                mocte.setFlag(1D);
                mocte.setTe001(moctc.getTc001());
                mocte.setTe002(moctc.getTc002());
                mocte.setTe003(String.format("%04d", xh));
                mocte.setTe004(moctb.getTb003());
                BigDecimal v = lingLiaoMapper.V_CLDL(moctb.getTb001(), moctb.getTb002(), moctb.getTb003());
                if(v == null) v = new BigDecimal(0D);
                // 剩余未领数量 moctb.getTb004() - moctb.getTb005() - v.doubleValue()
                BigDecimal sysl = new BigDecimal(moctb.getTb004()).subtract(new BigDecimal(moctb.getTb005())).subtract(new BigDecimal(v.doubleValue()));
                if(sysl.compareTo(new BigDecimal(0D)) < 0) {
                    sysl = new BigDecimal(0D);
                }
                BigDecimal bcll = null;  // 本次领料数量
                MOCTA one = moctaService.getOne(new LambdaQueryWrapper<MOCTA>().eq(MOCTA::getTA001, moctb.getTb001()).eq(MOCTA::getTA002, moctb.getTb002()));

                if(isMocta) {  // 走的 工单领料
                    bcll = new BigDecimal(moctb.getTb004());
                }else{   //排产领料，拿排产的数量
                    BigDecimal llNums = map.get(moctb.getUdf01().trim());
                    if(llNums == null) {
                        throw new CustomException("500", "找不到工单单身的排产数量");
                    }
                    if(one.getTA015().equals(new BigDecimal(0))) bcll = new BigDecimal(0);
                    else bcll = llNums.divide(one.getTA015(), 8, RoundingMode.HALF_UP).multiply(new BigDecimal(moctb.getTb004()));
                }
                if(bcll.compareTo(sysl) > 0) bcll = sysl;
                mocte.setTe005(bcll.setScale(Integer.parseInt(cmsma.getMa068()), RoundingMode.HALF_UP).doubleValue());  // 数量
////                        double ans = 0;
//                        BigDecimal ttt = new BigDecimal(moctb.getTb004() / one.getTA015().doubleValue() * one.getTA015().doubleValue());
//                        BigDecimal needNums = ttt.setScale(Integer.parseInt(cmsma.getMa068()), RoundingMode.HALF_UP);
////                        if(needNums.compareTo(new BigDecimal(yksl - ylUncheckNums)) < 0) {
////                            ans = needNums.doubleValue();
////                        }else{
////                            if(yksl - ylUncheckNums < 0) {
////                                ans = 0;
////                            }else{
////                                ans = new BigDecimal(yksl - ylUncheckNums).setScale(Integer.parseInt(cmsma.getMa068()), RoundingMode.HALF_UP).doubleValue();
////                            }
////                        }

                mocte.setTe006(moctb.getTb007());
                mocte.setTe007("");
                mocte.setTe008(moctb.getTb009());
                mocte.setTe009(moctb.getTb006());
                mocte.setTe010("********************");
                mocte.setTe011(moctb.getTb001());
                mocte.setTe012(moctb.getTb002());
                mocte.setTe013("");
                mocte.setTe014(moctb.getTb017());
                mocte.setTe015("");
                mocte.setTe016(moctb.getTb011());
                mocte.setTe017(moctb.getTb012());
                mocte.setTe018(moctb.getTb013());
                mocte.setTe019("N");
                mocte.setTe020("");
                mocte.setTe021(moctb.getTb019() - moctb.getTb020());
                mocte.setTe022(moctb.getTb021());
                mocte.setTe023("");
                mocte.setTe024(0D);
                mocte.setTe025("##########");
                mocte.setTe026(moctb.getTb007());
//                        ans = 0;
//                        if(needNums.compareTo(new BigDecimal(yksl - ylUncheckNums)) < 0) {
//                            ans = ttt.doubleValue();
//                        }else{
//                            if(yksl - ylUncheckNums < 0) {
//                                ans = 0;
//                            }else{
//                                ans = yksl - ylUncheckNums;
//                            }
//                        }
                mocte.setTe027(bcll.setScale(Integer.parseInt(cmsma.getMa068()), RoundingMode.HALF_UP).doubleValue());
                mocte.setTec01("2");
                mocte.setTeh01("N");
                mocte.setUdf51(yksl);
                mocteService.save(mocte);
                xh++;
            }
        }
        return res.toString();
    }

}
